/**
 * 全局参数维护
 * @author yaohuan
 * @date 2018/12/19
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {
            group: ''
        },
        cols = [[
            // {field: 'ROW_ID', width: 60, title: '序号'},
            {field: 'NAME', width: 160, title: '参数名称', fixed: 'left', sort: true, search: "default"}
            , {field: 'CODE', width: 80, title: '参数键', sort: true,search:"default"}
            , {field: 'VALUE', width: 80, title: '参数值'}
            , {
                field: 'GROUP_', width: 80, title: '所属分组',
                filterDatas: app.datas_('sys_params_group'),
                search: 'default',
                searchOpts: {type: 'select', default: ''}
            }
            , {field: 'REMARK', width: 137, title: '备注'},
            {
                width: 60,
                title: '',
                toolbar: '#script-paramsetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        paramSettingAction,
        param_edit_openOpts = {
            maxmin: true,
            area: ['720px', '265px'],
            title: '全局参数信息编辑'
        }
    ;

    var Param = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'param',
                _searchs: [{
                    id: 'param', tableId: 'param_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'param_edit', tpl: 'system/param_edit'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'param_table'),
                    url: g_ctxPath + 'sys/param/getParamslist',
                    exportsUrl: g_ctxPath + 'sys/param/exportsParam',
                    exportsName: '全局参数数据',
                    title: '全局参数信息表',
                    method: 'post',
                    id: 'param_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],//, 'exports'
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: ''
                });

                layui.laytpl(app.getByid(doc, 'script-paramsetting-action').innerHTML).render({}, function (string) {
                    paramSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(param_table)', function (obj) {

                    layer.tips(paramSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 5000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'param_edit',
                                            tagData: {
                                                obj: obj,
                                                detail: false
                                            },
                                            index: 0,
                                            openOpts: param_edit_openOpts
                                        });
                                        break;
                                    case '1':
                                        self.delParam(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(param_table)', function (obj) {
                    self._openTplFull({
                        tag: 'param_edit',
                        tagData: {
                            obj: obj,
                            detail: true
                        },
                        index: 0,
                        openOpts: param_edit_openOpts
                    });
                });

                //监听事件
                table.on('toolbar(param_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'param_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: param_edit_openOpts
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'param_table', where: table_param});
        },

        delParam: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'sys/param/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '全局参数信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'param',
                    methods: {}
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Param();

});